
const User = require('../model/user_model')




class UserService {
    async createUser({ user_id, username, password, nick_name }) {
        try {
            const res = await User.create({
                user_id,
                username,
                password,
                nick_name
            })
            return res.dataValues
        } catch (error) {
            console.error("创建用户错误", error)
        }
    }
    // 获取用户信息
    async getUserInfo({ user_id, username, password, is_admin }) {
        try {
            const whereOpt = {}

            user_id && Object.assign(whereOpt, { user_id })
            username && Object.assign(whereOpt, { username })
            password && Object.assign(whereOpt, { password })
            is_admin && Object.assign(whereOpt, { is_admin })

            const res = await User.findOne({
                attributes: ['user_id', 'username', 'password', 'is_admin'],
                where: whereOpt,
            })
            return res ? res.dataValues : null
        } catch (e) {
            console.error('获取用户信息失败', new Date(), e)
        }
    }
}

module.exports = new UserService()